//============================================================================
// Name        : ClassInfo.java
// Author      : iron.yin@gmail.com
// Version     : v1.0
// Copyright   : RisingSun CO,.LTD
// Description : ClassInfo.java in Java
//============================================================================
package cc.risingsun.wing.core;

import java.util.Map;
import java.util.Set;

import org.objectweb.asm.Type;

import cc.risingsun.wing.ds.DataStruct;

/**
 * 
 * @author iron.yin@gmail.com
 * @sina 2012-7-19
 */
public class WingClass extends WingBase {

	private final static String SUFFIX = "$IRON";
	private String internalName;
	private String className;
	private String[] interfaces;
	private Class<?> clazz;
	private Map<String, WingMethod> methods;
	private Map<String, WingField> fields;
	private Set<WingMethod> constructor;

	public WingClass(final int access, final String owner, final String name, final String[] interfaces) {
		super(owner, name);
		this.interfaces = interfaces.length == 0 ? ((access & ACC_INTERFACE) != 0 ? new String[] { name } : interfaces) : interfaces;
		clazz = ASMUtil.toClass(name);
		this.internalName = name.concat(SUFFIX);
		this.className = ASMUtil.reverse(internalName);
		methods = DataStruct.hashMap();
		fields = DataStruct.hashMap();
		constructor = DataStruct.hashSet();
	}

	/**
	 * @return the methods
	 */
	public Map<String, WingMethod> getMethods() {
		return methods;
	}

	public WingMethod getMethod(String name) {
		return methods.get(name);
	}

	public void addMethod(WingMethod method) {
		methods.put(method.getName(), method);
	}

	public String getClassName() {
		return className;
	}

	public String getInternalName() {
		return internalName;
	}

	public String[] getInterfaces() {
		return interfaces;
	}

	public Class<?> getClazz() {
		return clazz;
	}

	/**
	 * @return the fields
	 */
	public Map<String, WingField> getFields() {
		return fields;
	}

	public WingField getField(String name) {
		return fields.get(name);
	}

	public void addField(WingField field) {
		fields.put(field.getName(), field);
	}

	public WingField createField(String name, String desc) {
		final WingField field = new WingField(internalName, name, Type.getType(desc));
		fields.put(name, field);
		return field;
	}

	public WingMethod createMethod(String name, String desc, String[] exceptions) {
		Type type = Type.getMethodType(desc);
		WingParameter[] params = ASMUtil.getMethodParameter(type);
		final WingMethod method = new WingMethod(type, internalName, name, desc, params);
		method.setExceptions(exceptions);
		if (name.equals("<init>")) {
			constructor.add(method);
		} else {
			methods.put(name, method);
		}
		return method;
	}
}
